/**
 * 
 */
package cn.ivicar.passport.service;

import java.util.List;

import com.google.inject.Inject;

import cn.com.carsmart.exception.CarsmartException;
import cn.com.carsmart.ws.ibatis.model.BaseExample;
import cn.com.carsmart.ws.model.ValidateCode;
import cn.com.carsmart.ws.service.ValidateCodeManager;

/**
 * @Author: liurunbin
 * @Description:验证码service层
 * @Date: Created in 2017/5/16
 */
public class CaptchaService {

	@Inject
	private ValidateCodeManager validateCodeManager;

	public ValidateCode getByMobileAndType(String mobile, String serviceType, Integer useStatus) {
		ValidateCode code = new ValidateCode();
		code.setMobile(mobile);
		BaseExample example = validateCodeManager.getExample();
		example.or().andPropertyEqualTo(mobile, "mobile").andPropertyEqualTo(serviceType, "serviceType")
				.andPropertyEqualTo(useStatus, "useStatus");
		example.setOrderByClause("expired desc");
		List<ValidateCode> list = this.validateCodeManager.listByExample(code);
		if (list == null || list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	/*
	通过手机号获取验证码
	 */
	public ValidateCode getByMobileAndCode(String mobile, String captcha, String bizType) {
		ValidateCode code = new ValidateCode();
		code.setMobile(mobile);
		code.setValidateCode(captcha);
		code.setServiceType(bizType);
		List<ValidateCode> list = this.validateCodeManager.listByExample(code);
		if (list == null || list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	/*
	插入
	 */
	public int insert(ValidateCode validateCode) throws CarsmartException {
		validateCode.setUseStatus(0);
		int result = validateCodeManager.insert(validateCode);
		return result;
	}

	/*
	更新
	 */
	public void updateSelective(ValidateCode validateCode) throws CarsmartException {
		validateCodeManager.update(validateCode);

	}

}
